package com.p2p.service.contentManagementService.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.p2p.common.base.RespResult;
import com.p2p.common.view.CarouselVo;
import com.p2p.entity.Carousel;
import com.p2p.mapper.CarouselMapper;
import com.p2p.service.contentManagementService.CarouselService;
import com.p2p.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static com.p2p.utils.FileUtils.CAROUSEL_AVATAR;

/**
 * @author 93024
 */
@Service
@Slf4j
public class CarouselServiceImpl implements CarouselService {
    @Autowired
    CarouselMapper carouselMapper;

    @Override
    public RespResult addCarouselImg(MultipartFile img, Integer id, HttpServletRequest request) {
        try{
            String imgUrl = FileUtils.saveImage(img, request, CAROUSEL_AVATAR);
            int res = carouselMapper.insertCarouselImg(imgUrl,id);
            return RespResult.success("图片添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return RespResult.error("图片失败成功");
        }
    }

    @Override
    public RespResult addCarousel(Carousel carousel) {
        log.info("添加一条轮播广告");
        int insert = carouselMapper.insert(carousel);
        if (insert > 0){
            //返回插入数据的id
            int id = carousel.getId();
            return RespResult.success(id);
        }else {
            return RespResult.error("添加失败");
        }
    }

    @Override
    public RespResult deleteCarouselById(Integer id) {
        log.info("进入删除轮播广告===========");
        int i = carouselMapper.deleteById(id);
        if (i > 0){
            return RespResult.success();
        }else {
            return RespResult.error("删除失败");
        }
    }

    @Override
    public RespResult findCarouselByCondition(CarouselVo carouselVo) {

        log.info("进入条件查询轮播广告");
        QueryWrapper<Carousel> carouselQueryWrapper = new QueryWrapper<>();

        carouselQueryWrapper.eq(StringUtils.hasText(carouselVo.getTitle()), "title", carouselVo.getTitle())
                .eq(StringUtils.hasText(carouselVo.getLocation()), "location", carouselVo.getLocation())
                .eq(StringUtils.hasText(String.valueOf(carouselVo.getStatus())), "status", carouselVo.getStatus());
        Page<Carousel> page = new Page<>(carouselVo.getCurrentPage(), carouselVo.getPageSize());

        try{
            Page<Carousel> carouselList = carouselMapper.selectPage(page,carouselQueryWrapper);
            return RespResult.success(carouselList);
        }catch (Exception e){
            e.printStackTrace();
            return RespResult.error("查询失败");
        }
    }

    @Override
    public RespResult findAllCarousel(CarouselVo carouselVo) {
        log.info("进入查询全部轮播广告");

        Page<Carousel> page = new Page<>(carouselVo.getCurrentPage(), carouselVo.getPageSize());

        try{
            Page<Carousel> carouselList = carouselMapper.selectPage(page,null);
            return RespResult.success(carouselList);
        }catch (Exception e){
            e.printStackTrace();
            return RespResult.error("查询失败");
        }
    }

    @Override
    public RespResult findCarouselById(Integer id) {
        log.info("进入通过id轮播广告");
        try{
            Carousel carousel = carouselMapper.selectById(id);
            if (carousel != null){
                return RespResult.success(carousel);
            }else {
                return RespResult.success("没有此数据");
            }
        }catch (Exception e){
            e.printStackTrace();
            return RespResult.error("查询失败");
        }
    }

    @Override
    public RespResult updateCarousel(Carousel carousel) {
        log.info("进入修改轮播广告");
        int i = carouselMapper.updateById(carousel);
        if (i>0){
            return RespResult.success("修改成功");
        }else {
            return RespResult.error("修改失败");
        }
    }
}
